package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.RoleBean;
import com.project.service.IRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色controller
 */
@RequestMapping("role")
@RestController
public class RoleController {

    @Autowired
    private IRoleService roleService;

    //分页查询所有角色
    @RequiresPermissions("role:*")
    @RequestMapping("findByItem")
    IPage<RoleBean> findByItem(Integer pageNO) {
        return roleService.findByItem(pageNO);
    }

    //按照id删除角色
    @RequiresPermissions("role:*")
    @RequestMapping("del")
    public String del(Integer roleId) {
        roleService.del(roleId);
        return "ok";
    }

    //添加角色，同时添加角色拥有的权限集合
    @RequiresPermissions("role:*")
    @RequestMapping("add")
    public String add(RoleBean roleBean, Integer[] permission) {
        roleService.add(roleBean, permission);
        return "ok";
    }

    //根据角色id查询角色权限
    @RequiresPermissions("role:*")
    @RequestMapping("findById")
    public RoleBean findById(Integer roleId) {
        return roleService.findById(roleId);
    }

    //通过角色id修改用户权限，
    @RequiresPermissions("role:*")
    @RequestMapping("update")
    public String update(Integer roleId, Integer[] newArray) {
        roleService.update(roleId, newArray);
        return "ok";
    }

    //查询所有角色
    @RequiresPermissions("role:*")
    @RequestMapping("findAll")
    public List<RoleBean> findAll() {
        return roleService.findAll();
    }
}
